package com.xywl.escort.view.pay.project

import android.os.Bundle
import android.os.CountDownTimer
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.BaseResp
import com.xywl.escort.bean.MessageEvent
import com.xywl.escort.bean.PayConfirmReq
import com.xywl.escort.common.Constant
import com.xywl.escort.utils.ToastUtil
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_band_card_third_step.*
import org.greenrobot.eventbus.EventBus
import timber.log.Timber
import javax.inject.Inject

/**
 * 项目确认支付
 */
@Route(path = "/xy/pay/confirm")
class ProjectPayConfirmActivity : BaseActivity(), ProjectPayConfirmContract.View {

    private var isCountDown: Boolean = false
    @Autowired
    @JvmField
    var id = ""

    @Autowired
    @JvmField
    var payPwd = ""

    @Inject
    lateinit var mPresenter: ProjectPayConfirmContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_band_card_third_step)
        ARouter.getInstance().inject(this)
        showBackButton()
        setTitleText("支付确认")

        mPresenter.takeView(this)
        isCountDown = true
        btn_get_sms.isEnabled = false
        mVerificationTimer.start()
        addListener()
    }

    private fun addListener() {
        btn_get_sms.setOnClickListener {
            // 认购第二步-支付申请 获取短信验证码
            mPresenter.payApply(id, payPwd)
        }
        btn_confirm.setOnClickListener {
            submit()
        }
    }

    private fun submit() {
        val code = edit_sms_code.text.toString().trim()
        if (checkCode(code)) {
            val req = PayConfirmReq()
            req.id = id
            req.smsCode = code
            mPresenter.payConfirm(req)
        }
    }

    private fun checkCode(smsCode: String): Boolean {
        if (TextUtils.isEmpty(smsCode)) {
            ToastUtil.showTip(this, R.string.user_sms_code_empty)
            return false
        }
        return true
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T : Any?> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun payApplySuccess(msg: String?) {
        // 认购第二步-支付申请 成功
        ToastUtil.showTip(this, msg)
        isCountDown = true
        btn_get_sms.isEnabled = false
        mVerificationTimer.start()
    }

    override fun payApplyFailure(msg: String?) {
        // 认购第二步-支付申请 失败
        Timber.e("msg=$msg")
        ToastUtil.showTip(this, msg)
    }

    override fun payConfirmSuccess(resp: BaseResp?) {
        // 认购第三步-支付确认 成功
        ToastUtil.showTip(this, "支付成功")
        EventBus.getDefault().post(MessageEvent(Constant.EventType.EVENT_PAY_SUCCESS, Constant.EventMessage.PAY_SUCCESS))
        ARouter.getInstance()
                .build("/xy/pay/result")
                .withString("id", id)
                .withString("path", "/xy/order/subscribe/detail")
                .navigation()
        finish()
    }

    override fun payConfirmFailure(msg: String?) {
        // 认购第三步-支付确认 失败
        ToastUtil.showTip(this, msg)
    }

    private var mVerificationTimer = object : CountDownTimer((60 * 1000).toLong(), 1000) {
        override fun onTick(millisUntilFinished: Long) {
            btn_get_sms.isEnabled = false
            btn_get_sms.text = getString(R.string.account_auth_cd_format, millisUntilFinished / 1000)
        }

        override fun onFinish() {
            btn_get_sms.isEnabled = true
            btn_get_sms.text = getString(R.string.account_auth_re_get)
            isCountDown = false
        }
    }
}
